#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
vector<string> poke = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "joker", "JOKER"};
void Print(vector<string>& v) {
    for (auto& s : v) {
        cout << s << " ";
    }
}
bool isBoom(vector<string> str) {
    if (str.size() == 2) {
        if (str[0] == "JOKER" || str[0] == "joker") return true;
        else return false;
    } else if (str.size() == 4) {
        return true;
    }
    return false;
}
void compare(vector<string>& one, vector<string>& two) {
    auto o = find(poke.begin(), poke.end(), one[0]);
    auto t = find(poke.begin(), poke.end(), two[0]);
    if (o > t) {
        Print(one);
    } else {
        Print(two);
    }
}
int main() {
    string str;
    getline(cin, str);
    int pos = str.find('-');
    string left(str, 0, pos);
    string right(str, pos + 1);
    stringstream ss;
    ss << left;
    string tmp;
    vector<string> one, two;
    while (getline(ss, tmp, ' ')) {
        one.push_back(tmp);
    }
    ss.clear();
    ss << right;
    while (getline(ss, tmp, ' ')) {
        two.push_back(tmp);
    }
    if (one.size() == two.size()) {
        compare(one, two);
    } else {
        if (isBoom(one)) {
            if (isBoom(two)) {
                compare(one, two);
            } else {
                Print(one);
            }
        } else {
            if (isBoom(two)) {
                Print(two);
            } else {
                cout << "ERROR";
            }
        }
    }



    return 0;
}
